/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */
package com.fuyao.cloud.asset.purchaseorderlist.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyao.cloud.asset.cti.service.AssetCtiService;
import com.fuyao.cloud.asset.entity.AssetCti;
import com.fuyao.cloud.asset.entity.AssetPurchaseorder;
import com.fuyao.cloud.asset.entity.AssetPurchaseorderList;
import com.fuyao.cloud.asset.purchaseorderlist.mapper.AssetPurchaseorderListMapper;
import com.fuyao.cloud.asset.purchaseorderlist.service.AssetPurchaseorderListService;
import com.fuyao.cloud.asset.vo.purchaseorder.PurchaseOrderListVO;
import com.fuyao.cloud.common.data.datascope.DataScope;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 采购订单清单
 *
 * @author liyue
 * @date 2021-11-24 10:55:39
 */
@Service
public class AssetPurchaseorderListServiceImpl extends ServiceImpl<AssetPurchaseorderListMapper, AssetPurchaseorderList> implements AssetPurchaseorderListService {

	@Resource
	private AssetCtiService fyAssetCtiService;

	@Transactional
	@Override
	public boolean saveOrUpdatePurchaseOrderList(AssetPurchaseorder entity) {
		List<AssetCti> ctiList = fyAssetCtiService.list();
		List<AssetPurchaseorderList> purchaseOrderList = entity.getPurchaseOrderList();
		if (CollUtil.isNotEmpty(purchaseOrderList)) {
			purchaseOrderList.forEach(p -> {
				p.setStatus(Integer.parseInt(entity.getStatus()));
				p.setBatchnumber(entity.getBatchnumber());
				p.setParentId(entity.getId());
				p.setParentCaseid(entity.getCaseid());
				Optional<AssetCti> ctiOptional = ctiList.stream().filter(cti -> cti.getId().equals(p.getCtiId())).findFirst();
				if (ctiOptional.isPresent()) {
					AssetCti cti = ctiOptional.get();
					p.setCtiName(cti.getName());
					p.setClassifiedCode(cti.getFullcode());
					p.setClassified(cti.getFullname());
				}
			});
			return super.saveOrUpdateBatch(purchaseOrderList);
		}
		return Boolean.FALSE;
	}

	@Override
	public List<PurchaseOrderListVO> listPurchaseOrderList(AssetPurchaseorder entity) {
		List<AssetPurchaseorderList> purchaseOrderList = this.baseMapper.selectListByScope(Wrappers.<AssetPurchaseorderList>query().lambda().eq(AssetPurchaseorderList::getParentId,entity.getId()), new DataScope());
		if (CollUtil.isNotEmpty(purchaseOrderList)) {
			return purchaseOrderList.stream().map(t -> {
				PurchaseOrderListVO resultVO = new PurchaseOrderListVO();
				BeanUtil.copyProperties(t, resultVO);
				resultVO.setType(entity.getType());
				resultVO.setContractId(entity.getContractId());
				resultVO.setContractName(entity.getContractName());
				resultVO.setContractNumber(entity.getContractNumber());
				resultVO.setSupplierId(entity.getSupplierId());
				resultVO.setSupplierName(entity.getSupplierName());
				resultVO.setArrivalDatetime(entity.getArrivalDatetime());
				resultVO.setAmount(resultVO.getPrice().multiply(new BigDecimal(resultVO.getOrderQuantity())));
				resultVO.setMethod(entity.getMethod());
				resultVO.setCurrencyType(entity.getCurrencyType());
				return resultVO;
			}).collect(Collectors.toList());
		}
		return Lists.newArrayList();

	}
}
